package com.ruibang.glass.quality.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;

/**
 * @author 宋锦康
 */
@TableName("g_file")
@Data
@ApiModel(value = "文件对象", description = "文件对象")
public class FileEntity {

    /**
     * 主键ID
     */
    @ApiModelProperty(value = "主键ID")
    @TableId
    private String fileId;

    /**
     * 文件名
     */
    @ApiModelProperty(value = "文件名")
    private String fileName;

    /**
     * 文件标识MD5
     */
    @ApiModelProperty(value = "文件标识MD5")
    private String identifier;

    /**
     * 总大小
     */
    @ApiModelProperty(value = "总大小")
    private Long totalSize;

    /**
     * 地址
     */
    @ApiModelProperty(value = "地址")
    private String location;

    /**
     * 是否删除: 0.否 1.是
     */
    @ApiModelProperty(value = "是否删除: 0.否 1.是")
    private Integer delFlag;

    /**
     * 创建时间
     */
    @ApiModelProperty(value = "创建时间", hidden = true)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    /**
     * 文件大小带单位
     */
    @TableField(exist = false)
    @ApiModelProperty(value = "文件大小带单位")
    private String totalSizeName;

    /**
     * 文件类型
     */
    @ApiModelProperty(value = "文件类型(1:图片，2:视频)")
    private String type;


    @ApiModelProperty(value = "业务id")
    private String businessId;

    /**
     * 缩略图
     */
    @ApiModelProperty(value = "缩略图")
    private String thumbnail;

    @ApiModelProperty(value = "外风地址")
    @TableField(exist = false)
    private String remoteAddress;

    @ApiModelProperty(value = "本地地址")
    @TableField(exist = false)
    private String localAddress;


    public void setTotalSize(Long totalSize) {
        this.totalSize = totalSize;
        if (1024 * 1024 > this.totalSize && this.totalSize >= 1024) {
            this.totalSizeName = String.format("%.2f", this.totalSize.doubleValue() / 1024) + "KB";
        } else if (1024 * 1024 * 1024 > this.totalSize && this.totalSize >= 1024 * 1024) {
            this.totalSizeName = String.format("%.2f", this.totalSize.doubleValue() / (1024 * 1024)) + "MB";
        } else if (this.totalSize >= 1024 * 1024 * 1024) {
            this.totalSizeName = String.format("%.2f", this.totalSize.doubleValue() / (1024 * 1024 * 1024)) + "GB";
        } else {
            this.totalSizeName = this.totalSize.toString() + "B";
        }
    }

}
